package com.example.zlw.dem23;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @program: Zlw
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-11-09 22:09
 **/
public class Broker implements MethodInterceptor {
    //被代理的对象
    private Object target;
    public Broker(Object target) {
        this.target = target;
    }
    //获取代理对
    public Object getTalent(){
        //创建一个工具---生成代理对象
        Enhancer enhancer = new Enhancer();
        //指定被代理的的父类
        enhancer.setSuperclass(target.getClass());
        //代理的回调方法
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        greeting();
        //调用被代理对象的方法
        Object invoke = method.invoke(target, objects);
        thank();
        return invoke;
    }

    public void greeting(){
        System.out.println("鸡你太美");
        System.out.println("啊啊啊");
    }

    public void thank(){
        System.out.println("你干嘛！！！");
        System.out.println("叽叽叽叽");
    }

}

